//
//  B_HomeViewController.m
//  Application
//
//  Created by 薛小刚 on 2018/8/8.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_HomeViewController.h"
#import "ApplicationSetting.h"
#import "B_MineViewController.h"
#import "B_SelectNavView.h"
#import "B_ActivityListView.h"
#import "B_ActivityPictureView.h"
#import "B_TopicListView.h"
#import "B_TopicPictureView.h"
#import "B_TopicDetailVC.h"
#import "B_ActivityDetailVC.h"
#import "B_TopicPictureModel.h"
#import "B_ActivityPictureModel.h"


#import "B_SearchListVC.h"

@interface B_HomeViewController ()
/// 活动的大图模式
@property (nonatomic, strong) B_ActivityListView * acList;
/// 活动的大图模式
@property (nonatomic, strong) B_ActivityPictureView * acPicture;
/// 话题的列表模式
@property (nonatomic, strong) B_TopicListView * toList;
/// 话题的大图模式
@property (nonatomic, strong) B_TopicPictureView * toPicture;
/// 当前状态是活动还是话题  0活动 1话题
@property (nonatomic, assign) NSInteger nowIndex;


@end

@implementation B_HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf(weSelf, self);
    /// 添加左侧按钮
    [self setLeftImageBarButtonItemWithFrame:CGRectZero image:@"B_topic_home_search" selectImage:@"" action:^(GUIButton *button) {
        //TODO:执行搜索
        HXLog(@"搜索");
        B_SearchListVC * search = [[B_SearchListVC alloc] init];
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:search];
        [weSelf presentViewController:nav animated:YES completion:nil];
    }];
    _nowIndex = 0;
    [self setNavigation];
    // 设置导航中间的控件
    B_SelectNavView * navCen = [[B_SelectNavView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    self.navigationItem.titleView = navCen;
    navCen.titleArray = @[@"活动", @"话题"];
    navCen.tapActionWithIndex = ^(NSInteger index) {
        //TODO:执行切换活动和话题
        HXLog(@"点击了哪一个  %ld", index);
        weSelf.nowIndex = index;
        [weSelf setHomeView:index];
    };
    [self setHomeView:0];
    [[B_GaoDeManager shareGaoDeManager] startLocation:^(CLLocation *location) {
        /// dingwei 
    }];
}


- (void) setNavigation {
    WeakSelf(weSelf, self);
    if ([getShowView() isEqualToString:@"1"])  {
        [self setRightImageBarButtonItemWithImageArray:@[@"B_topic_home_share", @"B_topic_home_list"] action:^(GUIButton *button) {
            if (button.tag == 1) {  //TODO:切换图文显示模式
                if (getUserId()) {
                    setShowView(@"2");
                    [weSelf setNavigation];
                    [weSelf setHomeView:weSelf.nowIndex];
                }
            } else if (button.tag == 0) { //TODO:分享

            }
            HXLog(@"btn tag %ld", button.tag);
        }];
        
    } else if ([getShowView() isEqualToString:@"2"]) {
        [self setRightImageBarButtonItemWithImageArray:@[@"B_topic_home_share", @"B_topic_home_picture"] action:^(GUIButton *button) {
            if (button.tag == 1) {  //TODO:切换图文显示模式
                setShowView(@"1");
                [weSelf setNavigation];
                [weSelf setHomeView:weSelf.nowIndex];
            } else if (button.tag == 0) { //TODO:分享

            }
            HXLog(@"btn tag %ld", button.tag);
        }];
    }
}

- (void) setHomeView:(NSInteger) index {
    if (index == 0) {       // 活动
        if ([getShowView() isEqualToString:@"1"]) {     // 大图
            self.view = self.acPicture;
            if (self.acPicture.dataArray.count > 0) {
                
            } else {
                [self getActivityPictureData:YES];
            }
        } else if ([getShowView() isEqualToString:@"2"]) {      // 列表
            self.view = self.acList;
            [self getActivityListData:YES index:self.acList.selectedIndex];
        }
    } else {                // 话题
        if ([getShowView() isEqualToString:@"1"]) {     // 大图
            self.view = self.toPicture;
            if (self.toPicture.dataArray.count > 0) {
                
            } else {
                [self getTopicPictureData:YES];
            }
        } else if ([getShowView() isEqualToString:@"2"]) {      //列表
            self.view = self.toList;
            [self getTopicListData:YES index:self.toList.selectedIndex];
        }
    }
}
/// 活动列表
- (B_ActivityListView *)acList {
    if (!_acList) {
        WeakSelf(weSelf, self);
        /// 活动列表当前选中的下标 默认最新
        __block NSInteger selectIndex = 1;
        _acList = [[B_ActivityListView alloc] init];
        [_acList.tableView setTableViewHeaderFresh:^{
            //TODO:刷新
            HXLog(@"当前选择的下标是%ld", selectIndex);
//            [weSelf.acList.tableView endRefresh];
            [weSelf getActivityListData:YES index:selectIndex];
        }];
        [_acList.tableView setTableViewFooterFresh:^{
            //TODO:加载
            [weSelf getActivityListData:NO index:selectIndex];
//            [weSelf.acList.tableView endRefresh];
        }];
        _acList.tapAction = ^(NSInteger index) {
            //TODO:刷新数据
            selectIndex = index;
            if (index == 0 && weSelf.acList.attentionArray.count == 0) {
                [weSelf getActivityListData:YES index:index];
            } else if (index == 1 && weSelf.toList.attentionArray.count == 0) {
                [weSelf getActivityListData:YES index:index];
            } else if (index == 2 && weSelf.toList.attentionArray.count == 0) {
                [weSelf getActivityListData:YES index:index];
            }
            
        };
        _acList.tableView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            //TODO:cell的点击操作
            HXLog(@"点击了cell的下标为%ld", indexPath.section);
            
            B_ActivityPictureModel * model;
            if (selectIndex == 1) {
                model = weSelf.acList.newsArray[indexPath.section];
            } else if (selectIndex == 0) {
                model = weSelf.acList.attentionArray[indexPath.section];
            } else if (selectIndex == 2) {
                model = weSelf.acList.hottestArray[indexPath.section];
            }
            B_ActivityDetailVC * activity = [[B_ActivityDetailVC alloc] init];
            activity.activityId = model.id;
            [weSelf pushNextViewController:activity];
        };
    }
    return _acList;
}

/**
 活动大图

 @return 返回view
 */
- (B_ActivityPictureView *)acPicture {
    if (!_acPicture) {
         WeakSelf(weSelf, self);
        _acPicture = [[B_ActivityPictureView alloc] init];
        [_acPicture setTableViewHeaderFresh:^{
            [weSelf getActivityPictureData:YES];
        }];
        _acPicture.activityPictureLoadMore = ^{
            [weSelf getActivityPictureData:NO];
        };
        _acPicture.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_ActivityPictureModel * model = weSelf.acPicture.dataArray[indexPath.section];
            B_ActivityDetailVC * vc = [[B_ActivityDetailVC alloc] init];
            vc.activityId = model.id;
            [weSelf pushNextViewController:vc];
        };
    }
    return _acPicture;
}

-(B_TopicPictureView *)toPicture {
    if (!_toPicture) {
        WeakSelf(weSelf, self);
        _toPicture = [[B_TopicPictureView alloc] init];
        [_toPicture setTableViewHeaderFresh:^{
            //TODO:刷新
            [weSelf getTopicPictureData:YES];
        }];
        _toPicture.topicPictureLoadMore = ^{
            [weSelf getTopicPictureData:NO];
        };
        _toPicture.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_TopicPictureModel * model = weSelf.toPicture.dataArray[indexPath.section];
            B_TopicDetailVC * vc = [[B_TopicDetailVC alloc] init];
            vc.topicId = model.id;
            [weSelf pushNextViewController:vc];
        };
    }
    return _toPicture;
}
/// 话题
- (B_TopicListView *)toList {
    if (!_toList) {
        /// 活动列表当前选中的下标 默认最新
        WeakSelf(weSelf, self);
        
        __block NSInteger selectIndex = 1;
        _toList = [[B_TopicListView alloc] init];
        
        [_toList.tableView setTableViewHeaderFresh:^{
            //TODO:刷新
            HXLog(@"当前选择的下标是%ld", selectIndex);
            [weSelf getTopicListData:YES index:selectIndex];
        }];
        [_toList.tableView setTableViewFooterFresh:^{
            //TODO:加载
            [weSelf getTopicListData:NO index:selectIndex];
        }];
        _toList.tapAction = ^(NSInteger index) {
            //TODO:刷新数据
            selectIndex = index;
            if (index == 0 && weSelf.toList.attentionArray.count == 0) {
                [weSelf getTopicListData:YES index:index];
            } else if (index == 1 && weSelf.toList.attentionArray.count == 0) {
                [weSelf getTopicListData:YES index:index];
            } else if (index == 2 && weSelf.toList.attentionArray.count == 0) {
                [weSelf getTopicListData:YES index:index];
            }
        };
        _toList.tableView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            //TODO:cell的点击操作
            HXLog(@"点击了cell的下标为%ld", indexPath.section);
            B_TopicDetailVC * topic = [[B_TopicDetailVC alloc] init];
            B_TopicPictureModel * model;
            if (selectIndex == 1) {
                model = weSelf.toList.newsArray[indexPath.section];
            } else if (selectIndex == 0) {
                model = weSelf.toList.attentionArray[indexPath.section];
            } else if (selectIndex == 2) {
                model = weSelf.toList.hottestArray[indexPath.section];
            }
            topic.topicId = model.id;
            [weSelf pushNextViewController:topic];
        };
    }
    return _toList;
}



/**
 TODO:话题大图的数据请求

 @param isRefresh 是否刷新
 */
- (void) getTopicPictureData:(BOOL) isRefresh {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/topic/indexTopicList/%ld/10", self.index] parameters:nil isCache:isRefresh succeed:^(id data) {
        HXLog(@"data");
        [weSelf.toPicture endRefresh];
        if ([data[@"code"] intValue] == 20010) {
            if (isRefresh) {
                [weSelf.toPicture.dataArray removeAllObjects];
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_TopicPictureModel * model = [B_TopicPictureModel mj_objectWithKeyValues:dictionary];
                [weSelf.toPicture.dataArray addObject:model];
            }
            if (((NSArray *)data[@"values"]).count > 0) {
                [weSelf.toPicture.tableView reloadData];
            }
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.toPicture endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

/**
 TODO:话题列表的数据请求
 
 @param isRefresh 是否刷新
 */
- (void) getTopicListData:(BOOL) isRefresh index:(NSInteger) index {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/topic/selectTopicList/%ld/10/%@/%ld", (long)self.index, getUserId(), index + 1] parameters:nil isCache:isRefresh succeed:^(id data) {
        HXLog(@"data %@", data);
        [weSelf.toList.tableView endRefresh];
        if ([data[@"code"] intValue] == 20010) {
            if (isRefresh) {
                if (index == 0) {
                    [weSelf.toList.attentionArray removeAllObjects];
                } else if (index == 1) {
                    [weSelf.toList.newsArray removeAllObjects];
                } else {
                    [weSelf.toList.hottestArray removeAllObjects];
                }
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_TopicPictureModel * model = [B_TopicPictureModel mj_objectWithKeyValues:dictionary];
                if (index == 0) {
                    [weSelf.toList.attentionArray addObject:model];
                } else if (index == 1) {
                    [weSelf.toList.newsArray addObject:model];
                } else {
                    [weSelf.toList.hottestArray addObject:model];
                }
            }
            if (((NSArray *)data[@"values"]).count <= 0) {
                
            } else {
                [weSelf.toList.tableView.tableView reloadData];
            }
            
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.toList.tableView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

/**
 TODO:活动大图的数据请求
 
 @param isRefresh 是否刷新
 */
- (void) getActivityPictureData:(BOOL) isRefresh {
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/activity/indexActivityList/%ld/10", self.index] parameters:nil isCache:isRefresh succeed:^(id data) {
        HXLog(@"data %@", data);
        [weSelf.acPicture endRefresh];
        if ([data[@"code"] intValue] == 20010) {
            if (isRefresh) {
                [weSelf.acPicture.dataArray removeAllObjects];
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_ActivityPictureModel * model = [B_ActivityPictureModel mj_objectWithKeyValues:dictionary];
                [weSelf.acPicture.dataArray addObject:model];
            }
            if (((NSArray *)data[@"values"]).count > 0) {
                [weSelf.acPicture.tableView reloadData];
            }
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.acPicture endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

/**
 TODO:活动列表的数据请求
 
 @param isRefresh 是否刷新
 */
- (void) getActivityListData:(BOOL) isRefresh  index:(NSInteger) index{
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index++;
    }
    WeakSelf(weSelf, self);
    NSString * stringPath;
    if (getLocation().coordinate.longitude == 0 && getLocation().coordinate.latitude == 0) {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/activity/selectActivityList/%ld/10/%@/%ld/%.1f/%.1f", self.index, getUserId(), index == 2 ? index + 2 : index + 1, getLocation().coordinate.longitude, getLocation().coordinate.latitude];
    } else {
        stringPath = [NSString stringWithFormat:@"/yearnfor/inter/activity/selectActivityList/%ld/10/%@/%ld/%f/%f", self.index, getUserId(), index == 2 ? index + 2 : index + 1, getLocation().coordinate.longitude, getLocation().coordinate.latitude];
    }
    [NetworkingManager getNetworkRequestWithUrlString:stringPath parameters:nil isCache:isRefresh succeed:^(id data) {
        HXLog(@"data");
        [weSelf.acList.tableView endRefresh];
        if ([data[@"code"] intValue] == 20010) {
            if (isRefresh) {
                if (index == 0) {
                    [weSelf.acList.attentionArray removeAllObjects];
                } else if (index == 1) {
                    [weSelf.acList.newsArray removeAllObjects];
                } else {
                    [weSelf.acList.hottestArray removeAllObjects];
                }
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_ActivityPictureModel * model = [B_ActivityPictureModel mj_objectWithKeyValues:dictionary];
                if (index == 0) {
                    [weSelf.acList.attentionArray addObject:model];
                } else if (index == 1) {
                    [weSelf.acList.newsArray addObject:model];
                } else {
                    [weSelf.acList.hottestArray addObject:model];
                }
            }
            if (((NSArray *)data[@"values"]).count <= 0) {

            } else {
                [weSelf.acList.tableView.tableView reloadData];
            }
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.acList.tableView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
